package P1线性结构.LeetCode双指针;

public class LeetCode1013 {
    public boolean canThreePartsEqualSum(int[] arr) {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        if (sum % 3 != 0) {
            return false;
        }
        int key = sum / 3;
        int left = 0;
        int right = arr.length - 1;
        int leftSum = arr[left];
        int rightSum = arr[right];
        while (left + 1< right) {
            if (leftSum == key && rightSum == key) {
                return true;
            }
            if (leftSum != key) {
                left++;
                leftSum += arr[left];
            }
            if (rightSum != key) {
                right--;
                rightSum += arr[right];
            }
        }
        return false;
    }
}
